<template>
  <div>
    <Breadcrumb :opt-data="seriesOptData"></Breadcrumb>
    <PageLayout>
      <div class="table-container height-100">
        <EZTable
          v-loading="tableLoading"
          :table-id="tableId"
          :table-hdl="tableHdl"
          :rows="seriesData"
          :custom-empty="true"
          @pageChanged="getSeriesData"
          @sizeChanged="getSeriesData"
          @filterChange="getSeriesData">
          <EmptyContent slot="empty" :opt-data="seriesOptData"></EmptyContent>
        </EZTable>
      </div>
      <el-dialog
        :title="`${!isEditSeries ? '新增' : '编辑'}系列`"
        :visible.sync="showSeriesDialog"
        :close-on-click-modal="false"
        width="600px"
        center>
        <el-form
          v-loading="seriesFormLoading"
          :ref="seriesFormRef"
          :model="seriesForm"
          :rules="seriesFormRules"
          size="small"
          label-width="130px">
          <el-form-item label="设备厂家" prop="manufacturer">
            <el-select v-model="seriesForm.manufacturer" clearable filterable class="width-100"
                       placeholder="请选择设备厂家">
              <el-option :label="manufacturer.label" :value="manufacturer.value" :key="idx"
                         v-for="(manufacturer, idx) in manufacturerData"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="系列名" prop="name">
            <el-input v-model="seriesForm.name" placeholder="请填写系列名"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="seriesForm.remarks" placeholder="请填写备注"></el-input>
          </el-form-item>
          <div class="text-align-right">
            <el-form-item>
              <el-button size="medium" @click="seriesFormCanceled">取消</el-button>
              <el-button size="medium" type="primary" @click="seriesFormSubmitted">提交</el-button>
            </el-form-item>
          </div>
        </el-form>
      </el-dialog>
    </PageLayout>
  </div>
</template>

<script>
  import {tableIdConfig, tableOptBtnConfig} from '@/config/table';
  import {msgCode} from "@/config/string";
  import {confirmHandle, deepCopyWithJson, messageHandle} from "@/utils";
  import {assembleTableOptBtn, getTableConfig, TableStateHdl} from "@/modules/routeParamStateHdl";
  import seriesTableCfg from "../../config/table/series";
  import EZTable from '../../components/table/EZTable';

  export default {
    name: "DeviceSeries",
    components: {
      EZTable
    },
    data() {
      return {
        tableLoading: true,
        tableId: tableIdConfig.series,
        tableHdl: null,
        seriesData: [],
        showSeriesDialog: false,
        seriesFormLoading: false,
        manufacturerData: [],
        seriesForm: {},
        seriesFormRules: {
          manufacturer: [
            {required: true, message: '请选择设备厂家', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请填写系列名', trigger: 'blur'}
          ],
        },
        seriesFormRef: 'seriesFormRef',
        isEditSeries: false
      }
    },
    computed: {
      seriesOptData() {
        return [
          {
            type: 'btn',
            label: '新增系列',
            icon: 'icon-tianjia',
            callback: () => {
              this.clickCreateBtn();
            }
          }
        ];
      }
    },
    activated() {
      this.initTableConfig();
      this.initSeriesForm();
      this.getManufacturerData();
      this.getSeriesData();
    },
    methods: {
      initTableConfig() {
        if (this.tableHdl) return;
        const {columns = [], filters = []} = getTableConfig(this.tableId, seriesTableCfg);
        assembleTableOptBtn(columns, ({type = '', data = {}}) => {
          if (!type) return;
          const series = deepCopyWithJson(data);
          switch (type) {
            case tableOptBtnConfig.edit.type:
              this.showSeriesDialog = true;
              this.isEditSeries = true;
              this.seriesForm = series;
              break;
            case tableOptBtnConfig.delete.type:
              this.deleteSeries(series);
              break;
            default:
              break;
          }
        });
        this.tableHdl = new TableStateHdl(this.$route.name, this.tableId, columns, filters);
      },
      initSeriesForm() {
        this.seriesForm = {
          name: '',
          manufacturer: '',
          remarks: '',
        }
      },
      resetSeriesForm() {
        this.initSeriesForm();
        const seriesForm = this.$refs[this.seriesFormRef];
        seriesForm && seriesForm.resetFields();
      },
      clickCreateBtn() {
        this.isEditSeries && this.resetSeriesForm();
        this.showSeriesDialog = true;
        this.isEditSeries = false;
      },
      getManufacturerData() {
        API.manufacturer.manufacturerAll().then(result => {
          const {data = []} = result;
          this.manufacturerData = data;
        }).catch(err => {
          console.log('err', err)
        });
      },
      getSeriesData() {
        this.tableLoading = true;
        const selector = {};
        const tableParam = this.tableHdl.assembleTableParam();
        API.series.seriesList({...tableParam, selector}).then(result => {
          this.tableLoading = false;
          const {data = [], total = 0} = result;
          this.seriesData = data;
          this.tableHdl.total = total;
        }).catch(err => {
          console.log('err', err)
          this.tableLoading = false;
        });
      },
      deleteSeries(series = {}) {
        confirmHandle({label: `确定要删除系列：${series.name} 吗?`}).then(() => {
          API.series.seriesDelete({_id: series._id}).then(result => {
            messageHandle({code: msgCode.DELETE_SUCCESS, title: '系列'});
            this.tableHdl.judgeTableMatched();
            this.getSeriesData();
            this.resetSeriesForm();
          });
        }).catch(() => {
        });
      },
      seriesFormCanceled() {
        this.showSeriesDialog = false;
      },
      seriesFormSubmitted() {
        this.$refs[this.seriesFormRef].validate((valid) => {
          if (!valid) return;
          this.seriesFormLoading = true;
          if (!this.isEditSeries) {
            API.series.seriesAdd({...this.seriesForm}).then(result => {
              messageHandle({code: msgCode.ADD_SUCCESS, title: '型号'});
              this.showSeriesDialog = false;
              this.seriesFormLoading = false;
              this.getSeriesData();
              this.resetSeriesForm();
            }).catch(err => {
              console.log('err', err)
              this.seriesFormLoading = false;
            });
          } else {
            API.series.seriesEdit({...this.seriesForm}).then(result => {
              messageHandle({code: msgCode.EDIT_SUCCESS, title: '型号'});
              this.showSeriesDialog = false;
              this.seriesFormLoading = false;
              this.getSeriesData();
              this.resetSeriesForm();
            }).catch(err => {
              console.log('err', err)
              this.seriesFormLoading = false;
            });
          }
        });
      },
    },
  }
</script>

<style scoped>

</style>
